/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

import com.google.code.cubeirc.common.ApplicationInfo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class AboutForm extends Dialog {

	protected Object result;
	protected Shell shell;
	private Browser browser;
	private Button btnClose;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AboutForm(Shell parent, int style) {
		super(parent, style);
		setText(ApplicationInfo.APP_FULL);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		jbInit();
		this.shell.open();
		this.shell.layout();
		Display display = getParent().getDisplay();
		while (!this.shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return this.result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void jbInit() {
		this.shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.PRIMARY_MODAL);
		this.shell.setImage(SWTResourceManager.getImage(AboutForm.class, "/com/google/code/cubeirc/resources/ApplicationIcon.png"));
		this.shell.setSize(666, 467);
		this.shell.setText(getText());
		this.shell.setLayout(new FormLayout());
		
		this.browser = new Browser(this.shell, SWT.NONE);
		this.browser.setUrl("http://www.eclipse.org/legal/epl-v10.html");
		this.browser.setJavascriptEnabled(false);
		FormData fd_browser = new FormData();
		fd_browser.top = new FormAttachment(0);
		fd_browser.left = new FormAttachment(0);
		fd_browser.right = new FormAttachment(100);
		fd_browser.bottom = new FormAttachment(100, -102);
		this.browser.setLayoutData(fd_browser);
		
		this.btnClose = new Button(this.shell, SWT.NONE);
		btnClose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.dispose();
			}
		});
		this.btnClose.setImage(SWTResourceManager.getImage(AboutForm.class, "/com/google/code/cubeirc/resources/img_exit.png"));
		FormData fd_btnClose = new FormData();
		fd_btnClose.bottom = new FormAttachment(100);
		fd_btnClose.left = new FormAttachment(browser, 0, SWT.LEFT);
		this.btnClose.setLayoutData(fd_btnClose);
		this.btnClose.setText("Close");
		
		Display display = getParent().getDisplay();
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();

		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;

		shell.setLocation(x, y);

	}
}
